// ignore_for_file: unrelated_type_equality_checks

import 'dart:ffi';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/page/montage/montage_provider.dart';
import 'package:full_display/unit/asset_utils/asset_utils.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:full_display/unit/socket/udp/socket_udp_utils.dart';
import 'package:full_display/unit/toast/progress_hud.dart';
import 'package:provider/provider.dart';

class MAddClockDialogWidget extends StatelessWidget {
  const MAddClockDialogWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [ChangeNotifierProvider(create: (_) => MontageProvider())],
      child: const MAddClockDialog(),
    );
  }
}

class MAddClockDialog extends StatefulWidget {
  const MAddClockDialog({super.key});

  @override
  State<MAddClockDialog> createState() => _MAddClockDialogState();
}

class _MAddClockDialogState extends State<MAddClockDialog> {
  var _montageProvider = MontageProvider();
  TextEditingController openHVC = TextEditingController();
  TextEditingController openMvc = TextEditingController();

  TextEditingController closeHVC = TextEditingController();
  TextEditingController closeMvc = TextEditingController();

  @override
  void initState() {
    super.initState();
    _controllerAddListener();
  }

  ///通过监听，实时获取输入的内容
  _controllerAddListener() {
    openHVC.addListener(() {
      if (int.parse(openHVC.text) > 23) {
        FocusScope.of(context).requestFocus(FocusNode());
        openHVC.text = "23";
      }
    });
    openMvc.addListener(() {
      if (int.parse(openMvc.text) > 59) {
        FocusScope.of(context).requestFocus(FocusNode());
        openMvc.text = "59";
      }
    });
    closeHVC.addListener(() {
      if (int.parse(closeHVC.text) > 23) {
        FocusScope.of(context).requestFocus(FocusNode());
        closeHVC.text = "23";
      }
    });
    closeMvc.addListener(() {
      if (int.parse(closeMvc.text) > 59) {
        FocusScope.of(context).requestFocus(FocusNode());
        closeMvc.text = "59";
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    _montageProvider = context.watch<MontageProvider>();
    return Container(
      width: 320.0.W,
      height: 362.0.W,
      decoration: const BoxDecoration(
        gradient: LinearGradient(
            //渐变位置
            begin: Alignment.topCenter, //右上
            end: Alignment.bottomCenter, //左下
            //渐变颜色[始点颜色, 结束颜色]
            colors: [ColorPlate.app_blue, ColorPlate.sub_blue]),
        borderRadius: BorderRadius.all(Radius.circular(8.0)),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Padding(
            padding: EdgeInsets.fromLTRB(10.0.W, 20.0.H, 10.0.W, 20.0.H),
            child: Column(
              children: [
                Text(
                  "添加定时",
                  style: StandardTextStyle.textWhite17Style,
                  textAlign: TextAlign.center,
                ),
                Container(
                  height: 20.0.H,
                ),
                addClockTime("开机时间", openHVC, openMvc),
                Container(
                  height: 20.0.H,
                ),
                addClockTime("关机时间", closeHVC, closeMvc),
              ],
            ),
          ),
          Column(
            children: [
              Container(
                padding: EdgeInsets.only(top: 15.0.W),
                child: Divider(
                  height: 1.0.W,
                  color: ColorPlate.white, // 分割线颜色
                ),
              ),
              Container(
                height: 50.0.W,
                child: Row(
                  children: [
                    Expanded(
                      child: InkWell(
                        onTap: () {
                          if (openHVC.text.isEmpty || openMvc.text.isEmpty) {
                            ProgressHUD.showText("请输入开始时间");
                            return;
                          }
                          if (closeHVC.text.isEmpty || closeMvc.text.isEmpty) {
                            ProgressHUD.showText("请输入结束时间");
                            return;
                          }
                          String timeStr =
                              "SET:ALARM0:${int.parse(openHVC.text) * 3600 + int.parse(openMvc.text) * 60}:${int.parse(closeHVC.text) * 3600 + int.parse(closeMvc.text) * 60}";
                          // SocketUDPUtils().sendUDPMsgToIp(
                          // InternetAddress.anyIPv4, 123456, "");
                          Navigator.pop(context);
                        },
                        child: Center(
                          child: Text(
                            '确定',
                            style: StandardTextStyle.textWhite16Style,
                            textAlign: TextAlign.center,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget addClockTime(
      String title, TextEditingController hvc, TextEditingController mvc) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          padding: EdgeInsets.only(left: 10.0.W),
          child: Text(
            title,
            style: StandardTextStyle.textWhite17Style,
            textAlign: TextAlign.left,
          ),
        ),
        Container(
          height: 20,
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Container(
              padding: const EdgeInsets.fromLTRB(10.0, 0, 10.0, 0),
              width: 130.0.W,
              height: 50.0.W,
              decoration: BoxDecoration(
                //设置边框
                border: Border.all(color: Colors.white, width: 1),
                //背景颜色
                color: ColorPlate.backgroundOpacity0,
                //设置圆角
                borderRadius: BorderRadius.circular((25.0.W)),
              ),
              child: Row(
                children: [
                  Expanded(
                    child: Container(
                      height: 50.0.W,
                      child: TextField(
                          keyboardType: TextInputType.number,
                          style: const TextStyle(color: Colors.white),
                          controller: hvc,
                          decoration: InputDecoration(
                              hintText: '请输入',
                              hintStyle: StandardTextStyle.inputHintStyle,
                              border: InputBorder.none),
                          autofocus: true,
                          cursorColor: ColorPlate.white),
                    ),
                  ),
                  Container(
                    width: 5.0.W,
                  ),
                  Text(
                    "时",
                    style: StandardTextStyle.textWhite17Style,
                    textAlign: TextAlign.left,
                  ),
                ],
              ),
            ),
            Container(
              padding: const EdgeInsets.fromLTRB(10.0, 0, 10.0, 0),
              width: 130.0.W,
              height: 50.0.W,
              decoration: BoxDecoration(
                //设置边框
                border: Border.all(color: Colors.white, width: 1),
                //背景颜色
                color: ColorPlate.backgroundOpacity0,
                //设置圆角
                borderRadius: BorderRadius.circular((25.0.W)),
              ),
              child: Row(
                children: [
                  Expanded(
                    child: Container(
                      height: 50.0.W,
                      child: TextField(
                          keyboardType: TextInputType.number,
                          style: const TextStyle(color: Colors.white),
                          controller: mvc,
                          decoration: InputDecoration(
                              hintText: '请输入',
                              hintStyle: StandardTextStyle.inputHintStyle,
                              border: InputBorder.none),
                          autofocus: true,
                          cursorColor: ColorPlate.white),
                    ),
                  ),
                  Container(
                    width: 5.0.W,
                  ),
                  Text(
                    "分",
                    style: StandardTextStyle.textWhite17Style,
                    textAlign: TextAlign.left,
                  ),
                ],
              ),
            ),
          ],
        ),
      ],
    );
  }
}
